<?php
////////////////////////////////////////////////////
//	LinBoard
//	Framework/Thread.php
//	Created: 19th February 2009; 00:49 UTC
//	LinBoard (c) Copyright OpenInferno 2008, 2009
////////////////////////////////////////////////////

//Setup Environment
error_reporting(E_ALL | E_STRICT);

//Direct Access Check
if(!defined("LINBOARD"))
{
	die("<h1>Access Error</h1>Direct Access to this script is forbidden.");
}

class Thread
{
	public function getThreads($Boardid, $Startfrom = 0, $Limit = 30)
	{
		Framework::Mysql()->requireConnection();
		
		//Filter data
		if(isset($Boardid) && is_numeric($Boardid) && isset($Startfrom) && is_numeric($Startfrom) && isset($Limit) && is_numeric($Limit))
		{
			$Boardid = (int)$Boardid;
			$Startfrom = (int)$Startfrom;
			$Limit = (int)$Limit;
		} else {
			return false;
		}
		
		//Perform Query
		$result = Framework::Mysql()->query("SELECT * FROM `".Framework::Config()->Mysql['prefix']."threads` WHERE `parent` = {$Boardid} ORDER BY `latestpostime` DESC LIMIT {$Startfrom}, {$Limit};");
		if(Framework::Mysql()->numRows($result) < 1)
		{
			return false;
		}
		
		$Threads = array();
		while($row = Framework::Mysql()->fetchArray($result))
		{
			$Threads['id'][] = $row['id'];
			$Threads['posterid'][] = $row['posterid'];
			$Threads['title'][] = $row['title'];
			$Threads['description'][] = $row['description'];
			$Threads['latestpost'][] = $row['latestpost'];
			$Threads['parent'][] = $row['parent'];
			$Threads['latestpostime'][] = $row['latestpostime'];
			$Threads['posts'][] = $row['posts'];
		}
		
		return $Threads;
	}
	
	public function getThread($tid)
	{
		Framework::Mysql()->requireConnection();
		
		if(isset($tid) && is_numeric($tid))
		{
			$tid = (int)$tid;
		} else {
			return false;
		}
		
		//Perform Query
		$result = Framework::Mysql()->query("SELECT * FROM `".Framework::Config()->Mysql['prefix']."threads` WHERE `id` = {$tid} LIMIT 1;");
		if(Framework::Mysql()->numRows($result) < 1)
		{
			return false;
		}
		
		$Thread = array();
		$row = Framework::Mysql()->fetchArray($result);
		
		$Thread['id'] = $row['id'];
		$Thread['posterid'] = $row['posterid'];
		$Thread['title'] = $row['title'];
		$Thread['description'] = $row['description'];
		$Thread['latestpost'] = $row['latestpost'];
		$Thread['parent'] = $row['parent'];
		$Thread['latestpostime'] = $row['latestpostime'];
		$Thread['posts'] = $row['posts'];
		
		return $Thread;
	}
	
	public function addThread($pid, $title, $text, $description, $bid)
	{
		Framework::Mysql()->requireConnection();
		
		//Preserve Dirty Data
		$dirty_title = $title;
		$dirty_text = $text;
		
		//Filter Data
		if(isset($pid) && is_numeric($pid) && isset($title) && isset($text) && isset($description) && isset($bid) && is_numeric($bid))
		{
			$pid = (int)$pid;
			$title = Framework::Mysql()->escapeString($title);
			$text = Framework::Mysql()->escapeString($text);
			$description = Framework::Mysql()->escapeString($description);
		} else {
			return false;
		}
		
		$time = time();
		
		Framework::Mysql()->query("INSERT INTO `".Framework::Config()->Mysql['prefix']."threads` (`posterid`, `title`, `description`, `latestpost`, `parent`, `latestpostime`, `posts`) VALUES ('{$pid}', '{$title}', '{$description}', '0', '{$bid}', '{$time}', '0');");
		
		//Grab Thread ID
		$parent = Framework::Mysql()->lastInsertID(true);
		
		if(!Framework::Post()->writePost($pid, $dirty_title, $dirty_text, $parent))
		{
			return false;
		}
		
		//Grab Post ID
		$lpid = Framework::Mysql()->lastInsertID(true);
		
		Framework::Mysql()->query("UPDATE `".Framework::Config()->Mysql['prefix']."threads` SET `latestpost` = '{$lpid}' WHERE `id` = '{$parent}' LIMIT 1;");
		
		return true;
	}
	
	public static function getInstance()
	{
		static $instance;
		if(!isset($instance))
		{
			$class = __CLASS__;
			$instance = new $class;
		}
		return $instance;
	}
	
}

?>